package org.mj.im.common.id;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.InputStream;
import java.util.Properties;

public class SnowFlakeLoader {
    private static final Logger LOGGER = LoggerFactory.getLogger(SnowFlakeLoader.class);
    public static final String DATA_CENTER_ID = "data.center.id";
    public static final String MACHINE_ID = "machine.id";
    private volatile static Properties instances;
    static {
        try(InputStream in = SnowFlakeLoader.class.getClassLoader().getResourceAsStream("snowflake/snowflake.properties")){
            instances = new Properties();
            instances.load(in);
        }catch (Exception e){
            LOGGER.error("load snowflake properties error", e);
        }
    }
    private static String getStringValue(String key){
        if (instances == null){
            return "";
        }
        return instances.getProperty(key,"");
    }
    private static long getLongValue(String key){
        String v = getStringValue(key);
        return StringUtils.hasText(v) ? Long.parseLong(v) : 0;
    }
    public static Long getDataCenterId(){
        return getLongValue(DATA_CENTER_ID);
    }
    public static Long getMachineId(){
        return getLongValue(MACHINE_ID);
    }
}
